package at.fhv.dcs.aw.j2me.lcdui.event;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


/*
 * Created on 28.08.2007
 *
 */
public class EventMIDlet extends MIDlet implements CommandListener, ItemCommandListener, ItemStateListener {

    private Display m_display;
    private Form m_form;
    private TextField m_textField;    
    
    private Command m_clearCmd;
    private Command m_helpCmd;
    private Command m_exitCmd;
    
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    }

    protected void pauseApp() {        
        // go on alert will be shown after MIDlet resume
        
        Alert goOnAlert = new Alert("Go on...");
        goOnAlert.setTimeout(1000);
        m_display.setCurrent(goOnAlert);
    }

    protected void startApp() throws MIDletStateChangeException {
        if (m_display == null) {
            m_display = Display.getDisplay(this);
            
            m_textField = new TextField("Item", "a text", 20, TextField.ANY);            
            m_clearCmd = new Command("Clear", Command.ITEM, 1);            
            m_textField.setDefaultCommand(m_clearCmd);
            m_textField.setItemCommandListener(this);
            
            m_form = new Form("Input form");
            m_form.append(new StringItem("Event experiments...", null));
            m_form.append(m_textField);            
            m_helpCmd = new Command("Help", Command.HELP, 1);
            m_form.addCommand(m_helpCmd);
            
            m_exitCmd = new Command("Exit", Command.EXIT, 1);
            m_form.addCommand(m_exitCmd);
            
            m_form.setCommandListener(this);
            m_form.setItemStateListener(this);
            
            m_display.setCurrent(m_form);
        }
    }

    //displayable related command
    public void commandAction(Command cmd, Displayable d) {
        if (cmd == m_helpCmd && d == m_form) {
            System.out.println("Help...");
        } else if (cmd == m_exitCmd) {
            try {
                destroyApp(true);
            } catch (MIDletStateChangeException e) {
                e.printStackTrace();
            }
            notifyDestroyed();
        }
    }

    //item related command
    public void commandAction(Command cmd, Item item) {
        if (item == m_textField) {
            if (cmd == m_clearCmd) {
                m_textField.setString(null);
            }
        }
    }

    public void itemStateChanged(Item item) {
        if (item == m_textField) {
            System.out.println("Textfield changed:");
            System.out.println(m_textField.getString());
        }
    }
}
